gRPC হলো একটি উচ্চ পারফরম্যান্স RPC (Remote Procedure Call) ফ্রেমওয়ার্ক, যা Google দ্বারা তৈরি। এটি ডেটা আদান-প্রদানের জন্য Protobuf (Protocol Buffers) ফাইল ব্যবহার করে। gRPC HTTP/2 প্রোটোকল ব্যবহার করে এবং এটি বিভিন্ন প্ল্যাটফর্ম এবং ভাষায় সমর্থিত।
pom.xml
-এ gRPC এবং Protobuf-এর জন্য ডিপেন্ডেন্সি যোগ করুন:
<dependencies>
<!-- gRPC Starter -->
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-spring-boot-starter</artifactId>
<version>2.13.1</version>
</dependency>
<!-- Protobuf -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.21.12</version>
</dependency>
<!-- gRPC Java -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.56.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.56.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.56.0</version>
</dependency>
</dependencies>
Protobuf ফাইলটি gRPC সার্ভিস এবং মেসেজ গঠন সংজ্ঞায়িত করতে ব্যবহৃত হয়। উদাহরণস্বরূপ, user.proto
:
syntax = "proto3";
option java_package = "com.example.grpc";
option java_outer_classname = "UserProto";
service UserService {
rpc GetUser (UserRequest) returns (UserResponse);
}
message UserRequest {
int32 id = 1;
}
message UserResponse {
int32 id = 1;
string name = 2;
string email = 3;
}
Maven-এ Protobuf ফাইল জেনারেট করতে নিচের প্লাগইন যোগ করুন:
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.0</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocExecutable>/path/to/protoc</protocExecutable>
<pluginId>grpc-java</pluginId>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note: Ensure protoc
ইনস্টল করা আছে।
gRPC সার্ভারে Protobuf অনুযায়ী সার্ভিস ইমপ্লিমেন্ট করুন:
import com.example.grpc.UserProto.UserRequest;
import com.example.grpc.UserProto.UserResponse;
import com.example.grpc.UserServiceGrpc;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;
@GrpcService
public class UserServiceImpl extends UserServiceGrpc.UserServiceImplBase {
@Override
public void getUser(UserRequest request, StreamObserver<UserResponse> responseObserver) {
UserResponse response = UserResponse.newBuilder()
.setId(request.getId())
.setName("John Doe")
.setEmail("johndoe@example.com")
.build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}
gRPC ক্লায়েন্ট থেকে সার্ভারে কল করতে নিচের মতো কোড লিখুন:
import com.example.grpc.UserProto.UserRequest;
import com.example.grpc.UserProto.UserResponse;
import com.example.grpc.UserServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.springframework.stereotype.Service;
@Service
public class UserClient {
private final UserServiceGrpc.UserServiceBlockingStub userServiceStub;
public UserClient() {
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9090)
.usePlaintext()
.build();
userServiceStub = UserServiceGrpc.newBlockingStub(channel);
}
public UserResponse getUser(int id) {
UserRequest request = UserRequest.newBuilder().setId(id).build();
return userServiceStub.getUser(request);
}
}
Spring Boot অ্যাপ্লিকেশন চালিয়ে gRPC কল করুন:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class AppRunner implements CommandLineRunner {
@Autowired
private UserClient userClient;
@Override
public void run(String... args) {
System.out.println("Calling gRPC Server...");
var response = userClient.getUser(1);
System.out.println("Response: " + response.getName() + ", " + response.getEmail());
}
}
UserServiceImplBase
থেকে ইনহেরিট করে সার্ভিস অপারেশন ইমপ্লিমেন্ট করে।gRPC এবং Protobuf ব্যবহার করে Spring Boot-এ ডেটা পাঠানো খুবই কার্যকর, বিশেষত মাইক্রোসার্ভিস আর্কিটেকচারে। এটি high-performance, type-safe, এবং platform-independent সমাধান প্রদান করে। Protobuf ডেটার আকার ছোট করে এবং দ্রুত প্রসেসিং নিশ্চিত করে, যা gRPC-এর পারফরম্যান্স বাড়ায়।
Read more